package pro.shushi.pamirs.demo.core.util;

import org.apache.commons.collections4.CollectionUtils;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;

public class BigDecimalUtils {

    public static final BigDecimal oneHundred = new BigDecimal("100");

    // 四舍五入
    public static BigDecimal torRetainScale(BigDecimal source, int scale, int roundingMode) {
        return Optional.ofNullable(source)
                .map(_money -> _money.setScale(scale, roundingMode))
                .orElse(new BigDecimal(0));
    }

    /**
     * 保留2位小数.并且去掉尾部多余的0
     */
    public static String stripTrailingZerosStr(BigDecimal source, int scale, int roundingMode) {
        return torRetainScale(source, scale, roundingMode).stripTrailingZeros().toPlainString();
    }

    public static BigDecimal stripTrailingZeros(BigDecimal source, int scale, int roundingMode) {
        return torRetainScale(source, scale, roundingMode).stripTrailingZeros();
    }

    /**
     *   表达式list集合的BigDecimal求和操作
     */
    public static <T> BigDecimal sumBigDecimal(Collection<T> list, Function<T, BigDecimal> getFun) {
        if (CollectionUtils.isEmpty(list)) {
            return BigDecimal.ZERO;
        }

        return list.stream()
                .map(getFun)
                .filter(Objects::nonNull)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
    }

    /**
     * List BigDecimal 最后一个金额用减法
     */
    public static BigDecimal arrangeRestMoney(BigDecimal total, List<BigDecimal> subFeeList) {
        if (null == total || null == subFeeList) {
            return total;
        }
        subFeeList.removeIf(x -> null == x || BigDecimal.ZERO.compareTo(x) == 0);
        int length = subFeeList.size();
        if (length == 0) {
            return total;
        }
        BigDecimal sumFee = BigDecimal.ZERO;
        for (BigDecimal subFee : subFeeList) {
            sumFee = sumFee.add(subFee);
        }
        return total.subtract(sumFee);
    }

    /**
     * 返回小数位数
     * @param value
     * @return
     */
    public static int getNumberOfDecimalPlace(BigDecimal value) {
        if (value == null) {
            return 0;
        }
        value = value.stripTrailingZeros();
        final String s = value.toPlainString();
        final int index = s.indexOf('.');
        if (index < 0) {
            return 0;
        }
        return s.length() - 1 - index;
    }

    //是否为正整数
    public static Boolean isNotPositive(BigDecimal value) {
        if (value == null) {
            return false;
        }
        return new BigDecimal(value.intValue()).compareTo(value) != 0;
    }

    public static void main(String[] args) {
        System.out.println("0 = " + getNumberOfDecimalPlace(new BigDecimal(0 + "")));          // 0
        System.out.println("1.0 = " + getNumberOfDecimalPlace(new BigDecimal(1.0 + "")));      // 0
        System.out.println("1.01 = " + getNumberOfDecimalPlace(new BigDecimal(1.01 + "")));    // 2
        System.out.println("1.012 = " + getNumberOfDecimalPlace(new BigDecimal(1.012 + "")));  // 3
        System.out.println("0.01 = " + getNumberOfDecimalPlace(new BigDecimal(0.01 + "")));    // 2
        System.out.println("0.012 = " + getNumberOfDecimalPlace(new BigDecimal(0.012 + "")));  // 3
        System.out.println("0.0120 = " + getNumberOfDecimalPlace(new BigDecimal(0.012 + "")));  // 3
        System.out.println("705.000000 = " + getNumberOfDecimalPlace(new BigDecimal("705.000000")));//0
        System.out.println("705.000001 = " + getNumberOfDecimalPlace(new BigDecimal("705.000001")));//6
    }
}